package com.superdry.activity;

import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Global;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.drysurper.R;
import com.google.gson.Gson;
import com.superdry.adapter.CouponAdapter;
import com.superdry.json.Coupon;
import com.superdry.json.Userinfo;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;
import com.superdry.utils.ToastHelper;

public class CouponActivity extends AbActivity implements OnClickListener {

	View goback;
	ListView coupon_lv;
	CouponAdapter couponAdapter;
	public static String TAG = "http";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(R.layout.aty_coupon);
		initView();
	}

	public void initView() {
		goback = findViewById(R.id.goback_coupon);
		coupon_lv = (ListView) findViewById(R.id.coupon_lv);

		goback.setOnClickListener(this);
		coupon_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				// 判断是不是选择优惠券 进行操作
				if (getIntent() == null)
					return;
				double money = getIntent().getDoubleExtra("money", 0.0);
				Coupon coupon = couponAdapter.getItem(arg2);
				if (money > coupon.getMinPrice()) {
					//判断优惠券是否可用
					AbToastUtil.showToast(CouponActivity.this, "此优惠券不可用");
					return;
				}
				Intent intent = new Intent();
				intent.putExtra("coupon", coupon);
				setResult(0, intent);
				finish();
			}
		});

		Userinfo userInfo = SPHelper.getUserInfo(CouponActivity.this);
		if (null == userInfo) {
			AbDialogUtil.showAlertDialog(CouponActivity.this, "未登陆，请登录后查看！");
			return;
		}

		getCoupon(userInfo.getUserID());// 获取服务器数据
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.goback_coupon:

			finish();
			break;

		default:
			break;
		}
	}

	public void getCoupon(int userId) {

		AbHttpUtil http = AbHttpUtil.getInstance(CouponActivity.this);
		http.setTimeout(10000);
		AbRequestParams params = new AbRequestParams();
		params.put("userID", userId + "");
		Intent intent = getIntent();
		String url = GlobalConst.GET_USER_COUPON;

		if (intent != null && "choose_coupon".equals(intent.getStringExtra("cmd"))) {
			url = GlobalConst.GET_CHOOSE_COUPONS;
		}

		http.post(url, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {
				Log.i(TAG, "开始请求");
			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {

				AbToastUtil.showToast(CouponActivity.this, "获取优惠券信息失败");
			}
			@Override
			public void onSuccess(int statusCode, String content) {
				if (StringUtils.isEmpty(content))
					return;
				Coupon[] coupons = new Gson().fromJson(content, Coupon[].class);
				List<Coupon> couponList = Arrays.asList(coupons);
				couponAdapter = new CouponAdapter(couponList, CouponActivity.this);
				coupon_lv.setAdapter(couponAdapter);
			}
		});
	}
}
